<?php

	header('Content-type: text/html; charset=utf-8');
	echo "start321<br>";
	$pdo = null;
	function test1($pdo){
		$str1 = "INSERT INTO test1 (aaa, bbb, ccc) VALUES(?, ?, ?)";
		$stmt = $pdo->prepare($str1);
		$stmt->execute(['aaa','bbb','ccc']);
	}
	function test2($pdo){
		$str2 = "SELECT * FROM test1";
		$stmt = $pdo->query($str2);
		//print($stmt);
		//$stmt->execute();
		$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
		echo count($result);
		echo '<br>';
	    foreach ($result as $row) {
	        //echo $row['user_name'] . '<br>';
	        echo count($row).'<br>';
	    }
	}

	function initialDB($pdo){
		//$cars = array("1aaa","2bbb","3ccc","4ddd")
		//$cars[0] $cars[1] $cars[2] $cars[3] 
		$titles = array("我的世界",
				"天涯狂人",
				"每日气息",
				"明天的天气");
		$contents = array("了解我的内心",
				"刀客世界",
				"今天的不一样哦",
				"也是我的心情");
		$boFangNums = array("24","41","33","21");
		$dingYueNums = array(11,22,33,44);
		$str2 = "INSERT INTO bokeWen 
			(title, content, boFangNum, dingYueNum) 
			VALUES(?,?,?,? )";
		$stmt = $pdo->prepare($str2);
		for($i1=0; $i1<count($titles); $i1++){
			$stmt->execute([$titles[$i1], $contents[$i1], 
				$boFangNums[$i1], $dingYueNums[$i1],]);
		}

	}

	try {
	    $pdo = new PDO('sqlite:mydb1.db');
	    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


		$sql = "CREATE TABLE IF NOT EXISTS users (
		    id INTEGER PRIMARY KEY AUTOINCREMENT,
		    name TEXT NOT NULL,
		    email TEXT UNIQUE,
		    level INTEGER,
		    uId TEXT NOT NULL,
		    souc NUMERIC,
		    dingy NUMERIC,
		    remark1 TEXT,
		    remark2 TEXT,
		    remark3 TEXT,
		    remark4 TEXT,
		    createDate TEXT,
		    del TEXT
		)";
		$pdo->exec($sql);

		$sql1 = "CREATE TABLE IF NOT EXISTS tuiJian (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			name TEXT,
			title TEXT,
			content TEXT,
			conID TEXT
		)";
		$pdo->exec($sql1);

		$sql2 = "CREATE TABLE IF NOT EXISTS bokeWen (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			title TEXT,
			userName TEXT,
			content TEXT,
			boFangNum TEXT,
		    dingYueNum INTEGER,
		    picData TEXT,
		    remark1 TEXT,
		    remark2 TEXT,
		    remark3 TEXT,
		    remark4 TEXT,
		    del INTEGER
		)";
		$pdo->exec($sql2);

		$sql3 = "CREATE TABLE IF NOT EXISTS test1 (
		    id INTEGER PRIMARY KEY AUTOINCREMENT,
		    aaa TEXT,
		    bbb TEXT,
		    ccc TEXT
		)";
		$pdo->exec($sql3);

		$sql4 = "CREATE TABLE IF NOT EXISTS userDetail (
		    id INTEGER PRIMARY KEY AUTOINCREMENT,
		    uid TEXT,
		    bokeId INTEGER,
		    type1 TEXT, 
		    remark1 TEXT,
		    remark2 TEXT,
		    remark3 TEXT,
		    remark4 TEXT
		)";
		$pdo->exec($sql4);

		echo '<br>';
		foreach($pdo->query('SELECT * FROM test1') as $row){
			print_r($row);
			echo '<br>';
			echo json_encode($row);
			echo '<br>';
		}
		// $a=array("red","green");
		// array_push($a,"blue","yellow");
		// print_r($a);
		//test1($pdo);
		test2($pdo);
		//initialDB($pdo);
		echo '<br>';
		print_r((empty($pdo) ? 'true':'false').'<br>');
	} catch (PDOException $e) {
	    die("连接失败: " . $e->getMessage());
	}

    echo "start loop";

	$abc = array("aaa","bbb","ccc","ddd");
	for($i=0; $i<4; $i++){
		echo $abc[$i].'<br>';
	}
	echo "<br>";
	foreach($abc as $value){
		echo $value."<br>";
	}
	echo "<br>";
	for($i = 0; $i < count($abc); $i++){
		echo $abc[$i].'<br>';
	}
    echo 'end loop'.'<br>';
	echo 'ok';
	
/*

CREATE TABLE users (
    id         INTEGER PRIMARY KEY AUTOINCREMENT,
    name       TEXT    NOT NULL,
    email      TEXT    UNIQUE,
    level      INTEGER,
    uId        TEXT    NOT NULL,
    souc       NUMERIC AS (0),
    dingy      NUMERIC AS (0),
    remark1    TEXT,
    remark2    TEXT,
    remark3    TEXT,
    remark4    TEXT,
    createDate TEXT,
    del        TEXT    AS ('n') 
);

CREATE TABLE userDetail (
    id      INTEGER PRIMARY KEY AUTOINCREMENT,
    uid     INTEGER,
    bokeId  INTEGER,
    type1   TEXT,	s -> 收藏	b -> 播放
    remark1 TEXT,
    remark2 TEXT,
    remark3 TEXT,
    remark4 TEXT
);

*/
?>